<?php
declare (strict_types=1);

namespace app\api\controller;


use addons\V2\admin\model\OrderTemporary;
use app\common\model\Collect;
use app\common\model\Footprint;
use app\common\model\HistorySearch;
use app\common\model\News;
use app\common\tool\Result;
use addons\V2\admin\model\Product as ProductModel;
use think\Response;

class Product extends Base
{

    /**
     * get: 商品列表
     * path: ProductList
     * method: ProductList
     * param: keyword - {string} 搜索关键词
     * param: page - {string} 页数
     * param: cate_1 - {string} 一级分类
     * param: pick - {string} 推荐传1
     * param: hot - {string} 爆款传1
     * param: collect - {string} 收藏传1
     * param: footprint - {string} 足迹传1
     */
    public function ProductList(): \think\Response
    {
        try {
            $param = $this->request->param();
            $that = ProductModel::field('id,title,price,vip_price,cash,integral,icon,sales,pay_method');
            if (isset($param['cate_1'])) {
                $that->where('cate_1', $param['cate_1']);
            }
            if (isset($param['pick'])) {
                $that->where('pick', $param['pick']);
            }
            if (isset($param['hot'])) {
                $that->where('hot', $param['hot']);
            }
            if (isset($param['keyword'])) {
                $that->whereLike('title', '%' . $param['keyword'] . '%');
//                HistorySearch::setHistory($param['keyword'], $this->user->id);
            }
            if (isset($param['collect'])) {
                $pids = Collect::where(['uid'=>$this->user->id,'delete_time'=>0])->order('id', 'desc')->column('pid');
                $that->whereIn('id', $pids);
            }
//            if (isset($param['footprint'])) {
//                $pids = Footprint::where(['uid'=>$this->user->id,'delete_time'=>0])->order('id', 'desc')->column('pid');
//                $that->whereIn('id', $pids);
//            }
            $that->where(['status' => 1, 'delete_time' => 0]);

            $that->order('sort', 'asc')->order('create_time', 'desc');
            $that->page(intval($param['page'] ?? 1), 20);
            $list = $that->select();
            if (isset($list)) {
                return Result::success('获取成功', $list);
            } else {
                return Result::fail('获取失败');
            }
        } catch (\Exception $e) {
            return Result::success('获取成功', $e->getMessage());
        }
    }

    /**
     * get: 足迹
     * path: footprint
     * method: footprint
     */
    public function footprint()
    {
        $footprint = Footprint::where(['uid'=>$this->user->id,'delete_time'=>0])->group('create_time')->order('id', 'desc')->column('create_time');
        $data = array();
        foreach ($footprint as $k=>$v)
        {
            $data[]['product'] = Footprint::with(['product'])->where(['uid'=>$this->user->id,'delete_time'=>0,'create_time'=>$v])->select();
        }
        return Result::success('获取成功', $data);
    }


    /**
     * get: 商品详情
     * path: details
     * method: details
     * param: id - {string} id
     */
    public function details()
    {
        $id = $this->request->param('id');
        $info = ProductModel::where('id', $id)->field('id,title,cate_1,stock,price,vip_price,cash,content,images,sales,integral,pay_method')->find();
        $num = Collect::where(['delete_time'=>0,'uid'=>$this->user->id,'pid'=>$id])->count();
        if($num)
        {
            $info->collect = 1;
        }else{
            $info->collect = 0;
        }
        $info->content = handleRichText($info->content);
        $info->images = explode(',', $info->images);
        Footprint::setFootprint($id,$this->user->id);
        if (isset($info)) {
            return Result::success('获取成功', $info);
        } else {
            return Result::fail('获取失败');
        }
    }


    /**
     * post: 收藏
     * path: setCollect
     * method: setCollect
     * param: pid - {string} pid
     * param: type - {string} 1 收藏 2取消
     */
    public function setCollect()
    {
        $param = $this->request->param();
        $setCollect = Collect::setCollect($param['pid'],$param['type'],$this->user->id);
        return Result::fastReturn($setCollect);

    }


    /**
     * get:  兑换规则
     * path: exchange_rules
     * method: exchange_rules
     */
    public function exchange_rules(): \think\Response
    {
        $news = News::where(['delete_time'=>0,'status'=>1,'type'=>2])->find();
        $news->content = htmlspecialchars_decode($news->content);
        return Result::success('获取成功', $news);
    }


    /**
     * post: 购买商品
     * path: submit
     * method: submit
     * param: id - {string} id
     * param: num - {string} 数量
     */
    public function submit(): Response
    {
        if ($this->user->is_lock) {
            return Result::fail('账号已锁定');
        }
        $OrderTemporary = OrderTemporary::setTemporary($this->request->post(), $this->user);
        if($OrderTemporary['code']<1)
        {
            return Result::fail($OrderTemporary['msg']);
        }
        $temporary = OrderTemporary::getTemporary($this->user->id);
        if (md5('222222')==$this->user->pay_password) {
//            return Result::fastReturn(['code'=>2,'msg'=>'您的支付密码是初始密码：222222，是否前去修改支付密码']);
            $temporary['data']['default'] = 1;
        }else{
            $temporary['data']['default'] = 0;
        }
        if ($temporary['code'] != 1) return redirect('/');
        return Result::success('下单成功', $temporary['data']);

    }

    /**
     * get: 支付方式
     * path: payment_method
     * method: payment_method
     * param: ids - {string} ids
     */
    public function payment_method(): Response
    {
      $method[1] = ['title'=>'积分支付','pay_way'=>'integral', 'icon' => handleImg('/static/index/img/payway1.png')];
      $method[2] = ['title'=>'微信支付','pay_way'=>'wechat', 'icon' => handleImg('/static/index/img/payway2.png')];
      $method[3] = ['title'=>'微信+积分支付','pay_way'=>'wechat_integral', 'icon' => handleImg('/static/index/img/payway3.png')];
      $param = $this->request->param();
      $ids = explode(',',$param['ids']);
      $methods = [];
      foreach ($ids as $k=>$v)
      {
          $methods[] =  $method[$v];
      }
      return Result::success('获取成功', $methods);
    }



}

